<?php   // $Id: fact_bon_orph_suite.php 347 2012-05-07 09:11:55Z pcoustillas $ ?>
<?php
/*Copyright (C) 2003-2004 Guy Hendrickx
Licensed under the terms of the GNU  General Public License:http://www.opensource.org/licenses/gpl-license.php
File Authors:Guy Hendrickx
Modification : Pierre Coustillas*/

require_once("include/verif.php");

require_once("include/config/common.php");
require_once("include/config/var.php");
require_once("include/utils.php");


$date_facture=isset($_POST['date'])?$_POST['date']:"";
$date_facture = date_to_sql($date_facture);

$list_num=isset($_POST['bon_sup'])?$_POST['bon_sup']:"";
$acompte=isset($_POST['acompte'])?$_POST['acompte']:"";
$coment=isset($_POST['coment'])?$_POST['coment']:"";
$num=isset($_POST['num'])?$_POST['num']:"";
$client=isset($_POST['client'])?$_POST['client']:"";
$reference=isset($_POST['reference'])?$_POST['reference']:"";

$sql = "SELECT MAX(num) As Maxi FROM ${tblpref}facture";
$result = mysql_query($sql) or die('Erreur');
$num_fact = mysql_result($result, 0);
$num_fact = $num_fact + 1 ;
$sql = " SELECT nom, nom2 From ${tblpref}client WHERE num_client = $client ";
$req = mysql_query($sql) or die("Erreur SQL !<br/>$sql<br/>" . mysql_error());


$data = mysql_fetch_array($req);
$nom = $data['nom'];
$nom2 = $data['nom2'];

//liste des bons à facturer
if($list_num !='')
{
	$nb_bon=count($list_num);
	$list_num[$nb_bon]=$num;
} else {
	$list_num=array(0=>$num);
}

// Mettee à jour le num_facture de tout les bons sélectionnés
foreach ($list_num as $num_bon)
{
	$sql_bon = "UPDATE ${tblpref}bon_comm SET num_facture=$num_fact WHERE ${tblpref}bon_comm.client_num = '".$client."'
				AND num_bon=$num_bon";
	mysql_query($sql_bon) or die("Erreur sql_bon, mise à jour des bons !<br/>$sql_bon<br/>" . mysql_error());
}


//mettre à jour le smontant de la facture
$sql="SELECT sum(tot_htva) as sum_tot_ht FROM ${tblpref}bon_comm WHERE num_facture=$num_fact";
$result=mysql_query($sql) or die("Erreur SQL, montant facture !<br/>$sql<br/>" . mysql_error());
$sum_tot_ht=mysql_result($result,0);
$sum_tot_ht=is_null($sum_tot_ht)?0:$sum_tot_ht; //si la requete ne retourne rien faut prendre 0

$sql="SELECT sum(tot_tva) as sum_tot_tva FROM ${tblpref}bon_comm WHERE num_facture=$num_fact";
$result=mysql_query($sql) or die("Erreur SQL, montant facture !<br/>$sql<br/>" . mysql_error());
$sum_tot_tva=mysql_result($result,0);
$sum_tot_tva=is_null($sum_tot_tva)?0:$sum_tot_tva; //si la requete ne retourne rien faut prendre 0

$sum_tot_ttc=$sum_tot_ht + $sum_tot_tva;

//$sql_maj_fact="UPDATE ${tblpref}facture SET total_fact_h=$sum_tot_ht,total_fact_ttc=$sum_tot_ttc WHERE num=$num_fact";
//mysql_query($sql_maj_fact) or die("Erreur SQL, mise à jour facture !<br/>$sql_maj_fact<br/>" . mysql_error());
//echo $sql_maj_fact;

//créer la facture
$sql1 = "INSERT INTO ${tblpref}facture (num, acompte, coment, reference, client, date_fact, total_fact_h, total_fact_ttc)
				VALUES ('$num_fact','$acompte','$coment','$reference','$client', '$date_facture', '$sum_tot_ht', '$sum_tot_ttc')";
				mysql_query($sql1) or die("Erreur creation de la facture  <br/>$sql1<br/>" . mysql_error());


header("Location: fact_edit.php?num_fact=$num_fact");


